<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\model\Order;
use support\Request;
use support\Response;
use plugin\admin\app\model\OrderShippingNotice;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 提醒发货 
 */
class OrderShippingNoticeController extends Crud
{
    
    /**
     * @var OrderShippingNotice
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new OrderShippingNotice;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('order-shipping-notice/index');
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('order-shipping-notice/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('order-shipping-notice/update');
    }

    protected function formatNormal($items, $total): Response
    {
        $orderNos = array_column($items, 'order_no');
        $orders = Order::whereIn('order_no', $orderNos)->get()->toArray();
        $orders = array_combine(
            array_column($orders, 'order_no'),
            array_values($orders)
        );

        foreach ($items as $key=>$item) {
            $item['order_id'] = $orders[$item['order_no']]['id'];
            $items[$key] = $item;
        }

        return json(['code' => 0, 'msg' => 'ok', 'count' => $total, 'data' => $items]);
    }
}
